package org.zqh.mvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * 监听器，在初始化时读取mvc的配置文件
*    
* 项目名称：zqhmvc   
* 类名称：ActionListener   
* 类描述：   
* 创建人：zhangqh   
* 创建时间：2015年5月21日 下午11:03:41   
* 修改人：zhangqh  
* 修改时间：2015年5月21日 下午11:03:41   
* 修改备注：   
* @version    
*
 */
public class ActionListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("监听器注销");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("监听器启动");
		ServletContext context = arg0.getServletContext();
		try {
			context.setAttribute("zqhmvc", parseXml(context.getRealPath("\\") + context.getInitParameter("zqhmvc-config")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析mvc的xml配置文件
	 * @param mvcConfigFile
	 * @return
	 * @throws Exception
	 */
	private Map<String, ActionMapping> parseXml(String mvcConfigFile) throws Exception {
		Map<String, ActionMapping> map = new HashMap<String, ActionMapping>();
		// 引用jdom的SAXBuilder解析
		SAXBuilder sax = new SAXBuilder();
		Document document = sax.build(mvcConfigFile);
		Element rootElement = document.getRootElement();

		Element eFormBeans = rootElement.getChild("form-beans");
		Element eActionMapping = rootElement.getChild("action-mappings");

		List<Element> listAction = eActionMapping.getChildren();
		for (Element eAction : listAction) {
			ActionMapping mxc = new ActionMapping();
			mxc.setName(eAction.getAttributeValue("name"));
			mxc.setPath(eAction.getAttributeValue("path"));
			mxc.setType(eAction.getAttributeValue("type"));

			Map<String, String> mapForward = new HashMap<String, String>();
			List<Element> listForward = eAction.getChildren();
			for (Element eForward : listForward) {
				mapForward.put(eForward.getAttributeValue("name"), eForward.getAttributeValue("path"));
			}
			mxc.setActionForward(mapForward);

			List<Element> listFormBean = eFormBeans.getChildren();
			for (Element eForm : listFormBean) {
				if (mxc.getName().equals(eForm.getAttributeValue("name"))) {
					mxc.setName(eForm.getAttributeValue("type"));
					break;
				}
			}
			map.put(mxc.getPath(), mxc);
		}

		return map;
	}

}
